package com.example.hegel.goddess;

import android.net.Uri;

import com.google.common.base.Joiner;
import com.google.common.io.CharStreams;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;


public class FileService {

    public void saveUrl(OutputStream output, Set<String> urlSet) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"))) {
            writer.write(Joiner.on("\n").join(urlSet));
        }
    }

    public Set<String> loadUrl(InputStream input) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"))) {
            return new HashSet<>(CharStreams.readLines(br));
        }
    }

    public File getLastImage(Uri uri) {
        //再查找最近一天的目录下的文件
//        final File root = getImageRoot();
        final File root = new File(uri.getPath());
        if (!root.exists()) {
            return null;
        }

        final File[] images = root.listFiles();
        if (images.length > 0) {//
            return images[images.length - 1];
        }

        return null;
    }


}
